﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESOL.Common.Entity;
using ESOL.Business.Prod;
using ESOL.Common;

namespace ESOL.WinUI.Manufacture
{
    public partial class frmMaintainWorkcentersQuery : BaseForm.frmBaseDialogyQueryForm
    {
        #region vars
        public ProdWorkcentersEntity SelectedWorkcenter { get { return this.mSelectedWorkcenter; } }
        private ProdWorkcentersEntity mSelectedWorkcenter;
        #endregion

        #region ctors
        public frmMaintainWorkcentersQuery()
        {
            InitializeComponent();
        }
        #endregion

        #region functions
        private void BindCombobox()
        {
            UICommon.UICommonFunctions.BindComboboxByEnum(cmbType, typeof(WorkCentersType), typeof(int), true);
            UICommon.UICommonFunctions.BindGridComboboxByEnum(repositoryItemGridLookUpEdit1, typeof(WorkCentersType), typeof(int), false);

        }

        private string GetCondition()
        {
            string sCondition = "";
            if (!string.IsNullOrEmpty(txtName.Text))
            {
                sCondition += "and NAME='" + txtName.Text + "' ";
            }
            if (!string.IsNullOrEmpty(txtCode.Text))
            {
                sCondition += "and CODE='" + txtCode.Text + "' ";
            }
            if (!string.IsNullOrEmpty(cmbType.SelectedValue.ToString()))
            {
                sCondition += "and TYPE='" + cmbType.SelectedValue.ToString() + "' ";
            }
            if (cbActive.Checked)
            {
                sCondition += "and ACTIVE=1 ";
            }
            else
            {
                sCondition += "and ACTIVE=0 ";
            }
            return sCondition;
        }

        private void BindGrid()
        {
            string sCondition = GetCondition();
            List<ProdWorkcentersEntity> list = new List<ProdWorkcentersEntity>();
            list = ProdWorkcentersBs.GetAllForCondition(sCondition, "", "");
            gvMain.DataSource = list;
        }
        #endregion

        #region events
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindCombobox();
        }

        protected override void Click_Query()
        {
            BindGrid();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (gridView1.GetSelectedRows().Length == 0)
                return;
            mSelectedWorkcenter = (ProdWorkcentersEntity)gridView1.GetRow(gridView1.GetSelectedRows()[0]);
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (gridView1.GetSelectedRows().Length == 0)
            {
                MessageBox.Show("请选择一个工作中心！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            mSelectedWorkcenter = (ProdWorkcentersEntity)gridView1.GetRow(gridView1.GetSelectedRows()[0]);
            if (mSelectedWorkcenter == null)
            {
                MessageBox.Show("请选择一个工作中心！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                DialogResult = DialogResult.OK;
            }
        }
        protected override void Click_Confirm()
        {
            if (mSelectedWorkcenter == null)
            {
                MessageBox.Show("请选择一个工作中心！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                DialogResult = DialogResult.OK;
            }
        }

        #endregion
    }
}
